package com.uenpay.im.ui.old.activity.stickers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.aswife.ui.MaskImageView;
import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-1 下午1:23:16
 * @version 1.0
 * @function 添加表情适配器
 * 
 */
public class StickerAdapter extends BaseAdapter {

	private ArrayList<StickerEntity> arrayList = null;
	private Context mContext;
	private boolean is_manage = false;

	public StickerAdapter(Context _Context, ArrayList<StickerEntity> _ArrayList) {
		arrayList = _ArrayList;
		mContext = _Context;
	}
	
	public void setData(ArrayList<StickerEntity> _ArrayList){
		arrayList = _ArrayList;
		notifyDataSetChanged();
	}

	/**
	 * 设置是否整理
	 * 
	 * @param _is_manage
	 */
	public void setManage(boolean _is_manage) {
		is_manage = _is_manage;
		notifyDataSetChanged();
	}

	/**
	 * 获取删除个数
	 * 
	 * @return
	 */
	public int getDeleteCount() {
		int total = 0;
		for (int i = 0, size = arrayList.size(); i < size; i++) {
			if (arrayList.get(i).isSelected) {
				total++;
			}
		}
		return total;
	}

	/**
	 * 设置选中的表情状态
	 * 
	 * @param index
	 */
	public void setSelected(int index) {
		for (int i = 0, size = arrayList.size(); i < size; i++) {
			if (index == i) {
				arrayList.get(i).isSelected = !arrayList.get(i).isSelected;
				break;
			}
		}
		notifyDataSetChanged();
	}

	/**
	 * 获取选中的表情
	 * 
	 * @return
	 */
	public String getSelecedSticker() {
		String strs = "";
		for (int i = 0, size = arrayList.size(); i < size; i++) {
			if (arrayList.get(i).isSelected) {
				if (TextUtils.isEmpty(strs)) {
					strs = arrayList.get(i).favoriteid;
				} else {
					strs = strs + "," + arrayList.get(i).favoriteid;
				}
			}
		}
		return strs;
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		CustomFaceViewHodler customFaceViewHodler = null;
		StickerEntity mCustomEntity = arrayList.get(arg0);
		if (arg1 == null) {
			customFaceViewHodler = new CustomFaceViewHodler();
			arg1 = View.inflate(mContext, R.layout.item_sticker_layout, null);
			customFaceViewHodler.custom_face_iv = (MaskImageView) arg1
					.findViewById(R.id.custom_face_iv);
			customFaceViewHodler.seleced_status_iv = (ImageView) arg1
					.findViewById(R.id.seleced_status_iv);
			arg1.setTag(customFaceViewHodler);
		} else {
			customFaceViewHodler = (CustomFaceViewHodler) arg1.getTag();
		}

		if (TextUtils.isEmpty(mCustomEntity.customeFaceUrl)) {
			customFaceViewHodler.custom_face_iv
					.setImageResource(R.drawable.icon_add_no_fra);
		} else {
			customFaceViewHodler.custom_face_iv
					.SetUrl(mCustomEntity.customeFaceUrl);
		}

		if (is_manage) {
			customFaceViewHodler.seleced_status_iv.setVisibility(View.VISIBLE);
		} else {
			customFaceViewHodler.seleced_status_iv.setVisibility(View.GONE);
		}
		if (mCustomEntity.isSelected) {
			customFaceViewHodler.seleced_status_iv
					.setImageResource(R.drawable.icon_checked);
		} else {
			customFaceViewHodler.seleced_status_iv
					.setImageResource(R.drawable.icon_uncheck);
		}
		return arg1;
	}

	private class CustomFaceViewHodler {
		private ImageView seleced_status_iv;
		private MaskImageView custom_face_iv;
	}

}
